/*
* OnceFanDesService.java
* Created on  202015/7/22 8:59
* 版本       修改时间          作者      修改内容
* V1.0.1    202015/7/22       panzhuowen    初始版本
*
*/

package com.infitecs.eyas.suction.service;

import com.infitecs.eyas.boiler.dao.AirGasParameterDao;
import com.infitecs.eyas.boiler.dao.EfficiencyDao;
import com.infitecs.eyas.boiler.model.AirGasParameter;
import com.infitecs.eyas.boiler.model.Efficiency;
import com.infitecs.eyas.combustion.dao.BurnDao;
import com.infitecs.eyas.combustion.dao.ChimneyDao;
import com.infitecs.eyas.combustion.dao.CleanDao;
import com.infitecs.eyas.combustion.model.CalculateBurn;
import com.infitecs.eyas.combustion.model.CalculateChimney;
import com.infitecs.eyas.combustion.model.CalculateClean;
import com.infitecs.eyas.combustion.service.BurnService;
import com.infitecs.eyas.power.dao.OperateFieldDao;
import com.infitecs.eyas.power.model.OperateField;
import com.infitecs.eyas.result.dao.MiddleResultDao;
import com.infitecs.eyas.result.model.MiddleResult;
import com.infitecs.eyas.suction.dao.SuctionDesDao;
import com.infitecs.eyas.suction.model.SuctionDes;
import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.util.NumberUtil;
import com.infitecs.krill.exception.ValidateErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 类的描述信息
 *
 * @author panzhuowen
 * @version 1.0.1
 */
@Service
public class SuctionDesService {

    @Autowired
    private SuctionDesDao suctionDesDao;

    @Autowired
    private SessionService sessionService;
    @Autowired
    private EfficiencyDao efficiencyDao;
    @Autowired
    private ChimneyDao chimneyDao;
    @Autowired
    private CleanDao cleanDao;
    @Autowired
    private AirGasParameterDao airGasParameterDao;
    @Autowired
    private BurnService burnService;
    @Autowired
    private OperateFieldDao operateFieldDao;

    public void saveValueOfOnceFanDes(SuctionDes suctionDes) {
        suctionDes.setCaseId(sessionService.getCaseId());
        suctionDesDao.saveValueOfOnceFanDes(suctionDes);
        suctionDes.setParamName(sessionService.getE() + suctionDes.getParamName());
        sessionService.putParamMap(suctionDes);
    }

    public SuctionDes getOnceFanDes() {
        int caseId = sessionService.getCaseId();
        return suctionDesDao.getOnceFandes(caseId);
    }

    public void addOnceFanDes() {
        int caseId = sessionService.getCaseId();
        suctionDesDao.addOnceFanDes(caseId);
    }


    public void suctionDesSave(SuctionDes suctionDes) {
        if (suctionDes.getYfjglState().equals("0")) {
            Efficiency oldEfficiency = efficiencyDao.getEfficiencyByPowerCaseId(suctionDes.getCaseId());
            if (oldEfficiency == null) {
                throw new ValidateErrorException("锅炉数据未填写或未全部填写，不能引用锅炉数据！");
            }
        }
        if (suctionDes.getYfjrsState().equals("0")) {
            CalculateChimney calculateChimney = chimneyDao.getCalculateChimney(suctionDes.getCaseId());
            if (calculateChimney == null) {
                throw new ValidateErrorException("燃烧数据未填写或未全部填写，不能引用锅炉数据！");
            }
        }

        suctionDesDataSave(suctionDes);
    }

    public void suctionDesDataSave(SuctionDes suctionDes) {
        if (suctionDes.getYfjglState().equals("0")) {
            AirGasParameter airGasParameter = airGasParameterDao.getAirGasParameter(suctionDes.getCaseId());
            Efficiency efficiency = efficiencyDao.getEfficiencyByPowerCaseId(suctionDes.getCaseId());
            //获取锅炉参数
            //E3
            suctionDes.setE3(airGasParameter.getHearthOverOutAirCoefficient());
            //E4
            suctionDes.setE1(efficiency.getE93());
            //E6
            suctionDes.setE5(efficiency.getE52());
            //E8
            suctionDes.setE2(airGasParameter.getE28());
            //20
            suctionDes.setE4(airGasParameter.getAirPreheaterAirLeakageCoefficient());
        }
        if (suctionDes.getYfjrsState().equals("0")) {
            Map<String, Double> dataMap = burnService.getBurnDataByCaseId(suctionDes.getCaseId());
            CalculateClean calculateClean = cleanDao.getCalculateClean(suctionDes.getCaseId());
            CalculateChimney calculateChimney = chimneyDao.getCalculateChimney(suctionDes.getCaseId());
            suctionDes.setE6(NumberUtil.round(dataMap.get("E70") + dataMap.get("E137"), 4));
            suctionDes.setE7(dataMap.get("E104"));
            suctionDes.setE9(dataMap.get("E155"));
            suctionDes.setE8(calculateClean.getE248());
            suctionDes.setE9(calculateChimney.getE318());
        }

        SuctionDes oldData = suctionDesDao.getOnceFandes(suctionDes.getCaseId());
        if (oldData == null) {
            suctionDesDao.addSuctionDes(suctionDes);
        } else {
            suctionDesDao.updateSuctionDes(suctionDes);
        }
        OperateField operateField =new OperateField();
        operateField.setYfjglState(suctionDes.getYfjglState());
        operateField.setYfjrsState(suctionDes.getYfjrsState());
        operateField.setCaseId(suctionDes.getCaseId());
        operateFieldDao.updateOperateFieldSuctionDes(operateField);
    }
}
